package com.sense.kukoo.common.utils;

import static android.content.Context.NOTIFICATION_SERVICE;


import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.widget.RemoteViews;

import androidx.core.app.NotificationCompat;


import com.sense.kukoo.common.R;
import com.sense.kukoo.lib_logger.LoggerManager;
import com.sense.kukoo.lib_picture.loader.Monet;
import com.sense.kukoo.lib_picture.target.ITarget;

/**
 * <br> ClassName:   SenseNotificationManager.java
 * <br> Description:   通知管理类
 * <br> Author:      lionszhang
 * <br> Date:        2022/3/27
 */
public class SenseNotificationManager {
    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder mBuilder;
    private Notification notification;
    public static final int NOTIFICATION_ID = 10;

    private static SenseNotificationManager mSenseNotificationManager;

    public static SenseNotificationManager getInstance() {
        if (mSenseNotificationManager == null) {
            synchronized (SenseNotificationManager.class) {
                if (mSenseNotificationManager == null) {
                    mSenseNotificationManager = new SenseNotificationManager();
                }
            }
        }
        return mSenseNotificationManager;
    }

    public void sendNotification(PendingIntent pendingIntent, String title, String body, String iconUrl) {
        mNotifyManager = (NotificationManager) ApplicationContext.instance().getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "sense_app";
            String channelName = "sense_push";
            mNotifyManager.createNotificationChannel(new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH));
            mBuilder = new NotificationCompat.Builder(ApplicationContext.instance(), channelId);
        } else {
            mBuilder = new NotificationCompat.Builder(ApplicationContext.instance());
        }

        RemoteViews mRemoteViews = new RemoteViews(ApplicationContext.instance().getPackageName(), R.layout.common_view_notification);
        mRemoteViews.setTextViewText(R.id.title_tv, title);
        mRemoteViews.setTextViewText(R.id.content_tv, body);
        if (!TextUtils.isEmpty(iconUrl)) {
            Monet.get(ApplicationContext.instance()).load(iconUrl).asBitmap(new ITarget<Bitmap>() {
                @Override
                public boolean onResourceReady(Bitmap target) {
                    LoggerManager.e("SenseNotificationManager>>="+target);
//                    mBuilder.setLargeIcon(target);
                    mRemoteViews.setImageViewBitmap(R.id.head_icon, target);
                    mNotifyManager.notify(NOTIFICATION_ID, notification);
                    return false;
                }

                @Override
                public void onFailed(String message) {

                }
            });

        }
        mBuilder.setCustomContentView(mRemoteViews)
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())//对通知图标、时间戳、子文本和操作按钮使用系统装饰。
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setContentTitle(title)//系统合并通知栏自动显示
                .setContentText(body);//系统合并通知栏自动显示
        Uri notificationUrl = Uri.parse("android.resource://" + "com.sense.feel.common" + "/" + R.raw.notification);
        mBuilder.setSound(notificationUrl);
        int icon = ApplicationContext.instance().getApplicationInfo().icon;
        mBuilder.setSmallIcon(icon);

        mBuilder.setOnlyAlertOnce(true);
        mBuilder.setContentIntent(pendingIntent);
        notification = mBuilder.build();
        notification.sound = notificationUrl;
//        notification.defaults = Notification.DEFAULT_SOUND;
        notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        mNotifyManager.notify(NOTIFICATION_ID, notification);
    }
}
